#include "RailSketcher.h"

#include <iostream>

RailSketcher::RailSketcher(Rail * rail)
{
	this->rail = rail;
}

RailSketcher::~RailSketcher(void)
{
}

void RailSketcher::draw()
{
	Color * railLineColor = this->rail->getLineColor();
	Color * railBackgroundColor = this->rail->getElementColor();
	Texture * railTexture = this->rail->getTexture();

	list<Position*> * points = this->rail->getPoints();
	list<Position*>::iterator iterPoints;
	Position * startPoint;
	Position * endPoint;
	iterPoints = points->begin();
	endPoint = *iterPoints; 
	if(railTexture != NULL) {
		SketcherHelper::getInstance()->drawCircle(endPoint,this->rail->getHeight()/2,railTexture,railLineColor);
	}else {
		SketcherHelper::getInstance()->drawCircle(endPoint,this->rail->getHeight()/2,railBackgroundColor,railLineColor);
	}	
	while( ++iterPoints != points->end()) {		
		startPoint = endPoint; // Siempre se empieza de donde termina el otro.
		endPoint = *iterPoints;
		double dist = SketcherHelper::getInstance()->calcDist(*startPoint,*endPoint);
		double angle = SketcherHelper::getInstance()->calcAngle(*startPoint,*endPoint);
		if(dist != 0) {
			if(railTexture != NULL) {
				SketcherHelper::getInstance()->drawSquare(startPoint,dist,this->rail->getHeight(),angle,railTexture,railLineColor);
			}else {
				SketcherHelper::getInstance()->drawSquare(startPoint,dist,this->rail->getHeight(),angle,railBackgroundColor,railLineColor);
			}
		}
	}
	for(list<Position*> ::iterator point = points->begin();point != points->end(); point++){
	   if(railTexture != NULL) {
			SketcherHelper::getInstance()->drawCircle((*point),this->rail->getHeight()/2,railTexture,NULL);
		}else {
			SketcherHelper::getInstance()->drawCircle((*point),this->rail->getHeight()/2,railBackgroundColor,NULL);
		}
	}
		
}
